package com.polaris.live.adapter.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.adapter.HomeUserHeadAdapter
import com.polaris.live.adapter.live.BannerViewHolder
import com.polaris.live.adapter.view_holder.HomeListGameViewHolder
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.AdapterFriendShipBinding
import com.polaris.live.databinding.AdapterHomeListGameBinding
import com.polaris.live.databinding.AdapterListBannerBinding
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.GameVo
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.utils.HomeHeadItemDecoration
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.country.countryIcons
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * FriendShipAdapter
 *
 * @author Created by 半仙 on 2024/8/30/030 20:16
 */
class FriendShipAdapter(
    private val levelType: Int? = null,
    private val lifecycleScope: LifecycleCoroutineScope
) : BaseAdapter<Any, BaseViewHolder>(0) {


    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is DisplayUserBean -> CommonConst.HomeListType.TYPE_NORMAL
            is ListWithBannerBean -> CommonConst.HomeListType.TYPE_BANNER
            is GameVo -> CommonConst.HomeListType.TYPE_GAME
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            CommonConst.HomeListType.TYPE_BANNER -> BannerViewHolder(
                AdapterListBannerBinding.inflate(layoutInflater, parent, false), 8
            )

            CommonConst.HomeListType.TYPE_NORMAL -> FriendShipViewHolder(
                AdapterFriendShipBinding.inflate(layoutInflater, parent, false), levelType
            )

            CommonConst.HomeListType.TYPE_GAME -> HomeListGameViewHolder(
                AdapterHomeListGameBinding.inflate(layoutInflater, parent, false),
                lifecycleScope
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    class FriendShipViewHolder(binding: AdapterFriendShipBinding, levelType: Int? = null) :
        BaseBindingViewHolder<AdapterFriendShipBinding, DisplayUserBean>(binding) {

        private val mLevelType by lazy { levelType }

        override fun bind(item: DisplayUserBean) {
            mBinding.apply {
                Glide.with(ivUserCover.context).load(ImageUtils.imgPath(item.cover))
                    .transform(CenterCrop(), RoundedCorners(8.autoDp)).into(ivUserCover)

                Glide.with(ivRoomTypeCover.context).load(ImageUtils.imgPath(item.stateBackground))
                    .transform(RoundedCorners(8.autoDp)).into(ivRoomTypeCover)
                tvNickName.text = item.roomName

                pagView.setRoomFrame(item.base.roomFrame)
                if (item.base.country != null) {
                    val countryIcon = countryIcons[item.base.country]
                    if (countryIcon == null) {
                        llCountryBack.visibility = View.GONE
                    } else {
                        llCountryBack.visibility = View.VISIBLE
                        itemRoomCountry.setImageResource(countryIcon)
                    }
                } else {
                    llCountryBack.visibility = View.GONE
                }

                tvUserDetail.text = item.roomWelcomeSpeech ?: " "
                tvUserNum.text = item.stayUser.toString()

                widgetSubscript.setUserSymbol(item.symbol, showVis = View.INVISIBLE)

                when (item.roomType) {
                    LiveConst.RoomType.NORMAL -> {
                        roomTag.visibility = View.GONE
                        roomTag.setImageResource(0)
                    }

                    LiveConst.RoomType.AGENCY -> {  //公会
                        roomTag.visibility = View.VISIBLE
                        roomTag.setImageResource(if (AppCodeUtils.isAR()) R.drawable.family_tag_ar else R.drawable.family_tag)
                    }

                    LiveConst.RoomType.OFFICIAL -> { //官方
                        roomTag.visibility = View.VISIBLE
                        roomTag.setImageResource(if (AppCodeUtils.isAR()) R.drawable.sala_tag_ar else R.drawable.sala_tag)
                    }

                    else -> {
                        roomTag.visibility = View.GONE
                        roomTag.setImageResource(0)
                    }
                }
                vipView.setVipLevel(item.base.vipLevel)

                if (mLevelType == CHARM) {
                    widgetGameLevel.visibility = View.GONE
                    widgetLevel.setCharmsLevel(item.base.charms)
                } else if (mLevelType == GAME) {
                    setGameLevel(mBinding, item)
                } else {
                    val roomType = item.base.liveType
                    if (roomType == LiveConst.Type.PARTY_GAME_LIVE) {
                        setGameLevel(mBinding, item)
                    } else {
                        widgetGameLevel.visibility = View.GONE
                        widgetLevel.setCharmsLevel(item.base.charms)
                    }
                }
                headRecycler.apply {
                    adapter = HomeUserHeadAdapter().apply { setList(item.siteUsers?.asReversed()) }
                    layoutManager =
                        LinearLayoutManager(mBinding.root.context, LinearLayoutManager.HORIZONTAL, false).apply {
                            reverseLayout = true
                            stackFromEnd = true
                        }
                    itemAnimator = null
                    addItemDecoration(HomeHeadItemDecoration())
                }
            }
        }

        private fun setGameLevel(mBinding: AdapterFriendShipBinding, item: DisplayUserBean) {
            mBinding.apply {
                widgetLevel.visibility = View.GONE
                val gameLevel = item.gameLevel ?: 0
                if (gameLevel == 0) {
                    widgetGameLevel.visibility = View.GONE
                } else {
                    widgetGameLevel.visibility = View.VISIBLE
                }
                tvLevel.text = "Lv${gameLevel}"
            }
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is FriendShipViewHolder -> if (item is DisplayUserBean) holder.bind(item)
            is BannerViewHolder -> if (item is ListWithBannerBean) holder.bind(item)
            is HomeListGameViewHolder -> if (item is GameVo) holder.bind(item)
        }
    }

    companion object {
        const val CHARM = 1
        const val GAME = 2
    }
}